package com.lx.backstagemanagement.web;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.lx.backstagemanagement.RabbitMqProduct.SimpleProduct;
import com.lx.backstagemanagement.common.ResultCode;
import com.lx.backstagemanagement.entity.Product;
import com.lx.backstagemanagement.service.ICategoryService;
import com.lx.backstagemanagement.service.IProductService;
import com.lx.backstagemanagement.util.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.repository.query.Param;
import org.springframework.scheduling.annotation.Async;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping(value = "/product")
public class ProductController {
    @Autowired
    public FastFileStorageClient fastFileStorageClient;
    @Autowired
    private IProductService productService;
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private SimpleProduct simpleProduct;

    @GetMapping(value = "/{limit}/{pageSize}")
    public ResultCode getUsers(@PathVariable("limit") Integer limit, @PathVariable("pageSize") Integer pageSize) {
        if (StringUtils.isEmpty(limit)) {
            limit = 0;
        }
        Page<Product> products = productService.products(limit, pageSize);
        //users.getContent().stream().filter(entry -> "1".equals(entry.getUser_category())).collect(Collectors.toList());
        return ResultCode.getSuccessReturn(products);
    }

    @Async
    public String saveFile(InputStream in, long size, String filename) {
        return fastFileStorageClient.uploadImageAndCrtThumbImage(in, size, filename, null).getFullPath();
    }

    @PostMapping
    public ResultCode save(HttpServletRequest request, Product product, @Param("upload") MultipartFile upload, HttpSession httpSession) {
        if (upload.getSize() > 0) {
            try {
                String filename = saveFile(upload.getInputStream(), upload.getSize(), upload.getOriginalFilename().split("\\.")[1]);
                product.setPimage(filename);
            } catch (IOException e) {
                e.printStackTrace();
            }
            product.setPid(UUIDUtils.getUUID());
            product.setPflag(0);
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                product.setPdate(simpleDateFormat.parse(simpleDateFormat.format(date)));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            productService.save(product);
            simpleProduct.sender("product");
        } else {
            return ResultCode.SUCCESS;
        }
        return ResultCode.SUCCESS;
    }

    @GetMapping(value = "/{pid}")
    public ResultCode editUser(@PathVariable("pid") String pid) {
        if (StringUtils.isEmpty(pid)) {
            return null;
        }
        Product product = productService.getProductById(pid);
        return ResultCode.getSuccessReturn(product);
    }

    @DeleteMapping(value = "/{pid}")
    public ResultCode deleteUser(@PathVariable("pid") String pid) {

        productService.delete(pid);
        simpleProduct.sender("product");
        return ResultCode.SUCCESS;
    }

    @PutMapping()
    public ResultCode updateUser(HttpServletRequest request, @Param("upload") MultipartFile upload, Product product, String num) {
        if (upload.getSize() > 0) {
            try {
                StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(upload.getInputStream(), upload.getSize(), upload.getOriginalFilename().split("\\.")[1], null);
                product.setPimage(storePath.getFullPath());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        productService.update(product);

        return ResultCode.SUCCESS;
    }


}
